<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="支付平台" prop="platform">
        <el-select v-model="queryParams.platform" placeholder="请选择平台" clearable>
          <el-option label="支付宝" value="alipay" />
          <el-option label="微信支付" value="wechat" />
          <el-option label="银联" value="union" />
        </el-select>
      </el-form-item>
      <el-form-item label="配置名称" prop="configName">
        <el-input
          v-model="queryParams.configName"
          placeholder="请输入配置名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="isActive">
        <el-select v-model="queryParams.isActive" placeholder="请选择" clearable>
          <el-option label="启用" :value="1" />
          <el-option label="禁用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="环境" prop="environment">
        <el-select v-model="queryParams.environment" placeholder="请选择环境" clearable>
          <el-option label="开发环境" value="dev" />
          <el-option label="测试环境" value="test" />
          <el-option label="生产环境" value="prod" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:paymentConfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:paymentConfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:paymentConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:paymentConfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table 
      v-loading="loading" 
      :data="paymentConfigList" 
      @selection-change="handleSelectionChange"
      stripe
      border
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="支付平台" align="center" prop="platform" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.platform === 'alipay'" type="primary" size="small">支付宝</el-tag>
          <el-tag v-else-if="scope.row.platform === 'wechat'" type="success" size="small">微信支付</el-tag>
          <el-tag v-else-if="scope.row.platform === 'union'" type="warning" size="small">银联</el-tag>
          <el-tag v-else size="small">{{ scope.row.platform }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="配置项名称" align="center" prop="configName" min-width="150" show-overflow-tooltip />
      <el-table-column label="配置项值" align="center" prop="configValue" min-width="200" show-overflow-tooltip />
      <el-table-column label="配置说明" align="center" prop="configDescription" min-width="200" show-overflow-tooltip />
      <el-table-column label="是否启用" align="center" prop="isActive" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isActive"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
            v-hasPermi="['system:paymentConfig:edit']"
          />
        </template>
      </el-table-column>
      <el-table-column label="环境" align="center" prop="environment" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.environment === 'prod'" type="danger" size="small">生产</el-tag>
          <el-tag v-else-if="scope.row.environment === 'test'" type="warning" size="small">测试</el-tag>
          <el-tag v-else-if="scope.row.environment === 'dev'" type="info" size="small">开发</el-tag>
          <span v-else>{{ scope.row.environment }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:paymentConfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:paymentConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改支付平台配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-tabs v-model="activeTab" type="border-card">
          <!-- 基本信息 -->
          <el-tab-pane label="基本信息" name="basic">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="支付平台" prop="platform">
                  <el-select v-model="form.platform" placeholder="请选择平台" style="width: 100%">
                    <el-option label="支付宝" value="alipay" />
                    <el-option label="微信支付" value="wechat" />
                    <el-option label="银联" value="union" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="环境" prop="environment">
                  <el-select v-model="form.environment" placeholder="请选择环境" style="width: 100%">
                    <el-option label="开发环境" value="dev" />
                    <el-option label="测试环境" value="test" />
                    <el-option label="生产环境" value="prod" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="配置项名称" prop="configName">
                  <el-input v-model="form.configName" placeholder="如：app_id, private_key等" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="配置项值" prop="configValue">
                  <el-input 
                    v-model="form.configValue" 
                    type="textarea"
                    :rows="6"
                    placeholder="请输入配置项值"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="配置说明" prop="configDescription">
                  <el-input 
                    v-model="form.configDescription" 
                    type="textarea"
                    :rows="3"
                    placeholder="请输入配置项说明"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否启用" prop="isActive">
                  <el-radio-group v-model="form.isActive">
                    <el-radio :label="1">启用</el-radio>
                    <el-radio :label="0">禁用</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPaymentConfig, getPaymentConfig, delPaymentConfig, addPaymentConfig, updatePaymentConfig } from "@/api/system/paymentConfig";

export default {
  name: "PaymentConfig",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      paymentConfigList: [],
      title: "",
      open: false,
      activeTab: 'basic',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        platform: null,
        configName: null,
        configValue: null,
        configDescription: null,
        isActive: null,
        environment: null,
        createdAt: null,
        updatedAt: null
      },
      form: {},
      rules: {
        platform: [
          { required: true, message: "支付平台不能为空", trigger: "change" }
        ],
        configName: [
          { required: true, message: "配置项名称不能为空", trigger: "blur" }
        ],
        configValue: [
          { required: true, message: "配置项值不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listPaymentConfig(this.queryParams).then(response => {
        this.paymentConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 启用状态切换 */
    handleStatusChange(row) {
      let text = row.isActive === 1 ? "启用" : "禁用";
      this.$modal.confirm('确认要"' + text + '""' + row.configName + '"配置吗？').then(() => {
        return updatePaymentConfig(row);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.getList();
      }).catch(() => {
        row.isActive = row.isActive === 0 ? 1 : 0;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        platform: null,
        configName: null,
        configValue: null,
        configDescription: null,
        isActive: 1,
        environment: 'prod',
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset();
      this.activeTab = 'basic';
      this.open = true;
      this.title = "添加支付平台配置";
    },
    handleUpdate(row) {
      this.reset();
      this.activeTab = 'basic';
      const id = row.id || this.ids
      getPaymentConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改支付平台配置";
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePaymentConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPaymentConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除支付平台配置编号为"' + ids + '"的数据项？').then(function() {
        return delPaymentConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      this.download('system/paymentConfig/export', {
        ...this.queryParams
      }, `paymentConfig_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
::v-deep .el-tabs--border-card {
  box-shadow: none;
}

::v-deep .el-tabs__content {
  padding: 20px;
}
</style>
